const Controller = require('egg').Controller
// console.log('---Controller-', Controller)
class users extends Controller {
  async register() {
    const {
      ctx,
      app,
      service
    } = this;
    try {
      let {
        userName,
        workNumber,
        passwd,
        phone,
        userId,
        aroleid
      } = ctx.request.body;
      let params = {
        userName,
        workNumber,
        passwd,
        phone,
        aroleid: aroleid || 1 // 超级管理员
      }
      // 参数校验
      if (!ctx.helper.isNullFiled(params)) {
        return
      }
      params.userId = userId;
      // 查询改用户是否已经注册
      let result = await service.admin.user.create(params);
      ctx.body = result;

    } catch (error) {
      console.log(error)
    }
  }

  async addUser() {
    const {
      ctx,
      app,
      service
    } = this;
    try {
      let {
        userName,
        workNumber,
        passwd,
        phone,
        userId,
        roleType
      } = ctx.request.body;
      let params = {
        userName,
        workNumber,
        passwd,
        phone,
        aroleid: roleType || 1 // 超级管理员
      }
      // 参数校验
      if (!ctx.helper.isNullFiled(params)) {
        return
      }
      params.userId = userId;
      params.type = 'addUser'
      // 查询改用户是否已经注册
      let result = await service.admin.user.create(params);
      ctx.body = result;

    } catch (error) {
      console.log(error)
    }
  }
  async modifyUser() {
    const {
      ctx,
      app,
      service
    } = this;
    try {
      let {
        auserid,
        phone,
        userId,
        roleType,
        userName,
        passwd,
        type
      } = ctx.request.body;
      let params = {
        auserid,
        userId,
        phone,
        aroleid: roleType,  // 超级管理员
        userName,
        passwd,
        type
      }
      // 参数校验
      if (!ctx.helper.isNullFiled({ auserid })) {
        return
      }
      // params.userId = userId;
      // params.type = 'addUser'
      // 查询改用户是否已经注册
      let result = await service.admin.user.userUpDate(params);
      ctx.body = result;

    } catch (error) {
      console.log(error)
    }
  }

  async delUser() {
    const {
      ctx,
      app,
      service
    } = this;
    try {
      let {
        auserid,
        userId,
      } = ctx.request.body;
      let params = {
        auserid,
        userId,
        type: 'DEL'
      }
      // 参数校验
      if (!ctx.helper.isNullFiled(params)) {
        return
      }
      // params.userId = userId;
      // params.type = 'addUser'
      // 查询改用户是否已经注册
      let result = await service.admin.user.userUpDate(params);
      ctx.body = result;

    } catch (error) {
      console.log(error)
    }
  }

  async login() {
    const {
      ctx,
      service
    } = this, {
      account,
      passwd,
      newPwd,
      operationType
    } = ctx.request.body;

    if (!ctx.helper.isNullFiled({
      account,
      passwd
    })) {
      return
    }
    if(operationType==='modifyPwd'&&!newPwd){
      ctx.body = {
        code:10101,
        msg:'新密码不能为空',
      }
      return
    }
    let res = await service.admin.user.login({
      account,
      passwd,
      operationType,
      newPwd,
    })
    ctx.body = res
  }
  /**
   * 退出登录
   */
  async loginOut() {
    const {
      ctx,
      service
    } = this;
    // let res = await service.user.getMenuList();
    ctx.cookies.set('token', '');
    ctx.body = {
      code: 10301,
      msg: '已退出登录'
    };


  }
  /**
   * 获取菜单列表
   */
  async getMenuList() {
    const {
      ctx,
      service,
    } = this;
    let { userId, tokenRoleType } = ctx.request.body;
    console.log('%c gc_tokenRoleType=>: ', 'color: MidnightBlue; background: Aquamarine; font-size: 20px;', tokenRoleType);
    let res = await service.admin.user.getMenuList(userId, tokenRoleType);
    ctx.body = res;

  }
  /**
   * 添加菜单
   */
  async menuAddMenu() {
    const {
      ctx,
      service
    } = this;
    let {
      name,
      component,
      path,
      redirect,
      meta,
      parentid,
      userId,
    } = ctx.request.body, params = {
      name,
      component,
      path,
      redirect,
      meta,
      parentid,
      userId,
    }
    //参数校验
    if (!ctx.helper.isNullFiled({
      name,
      component,
      path,
      meta,
      parentid,
      userId,
    })) {
      return false;
    }
    let res = await service.admin.user.menuAddMenu(params);
    ctx.body = res;

  }

  async MenuModify() {
    const {
      ctx,
      service
    } = this;
    let {
      name,
      component,
      path,
      redirect,
      meta,
      id,
      userId,
      aroleid,
    } = ctx.request.body, params = {
      name,
      component,
      path,
      redirect,
      meta,
      id,
      userId,
      aroleid,
    }
    if (!id && id != 0) {
      return {
        code: 10201,
        msg: 'id不能为空'
      }
    }
    //参数校验
    if (!ctx.helper.isNullFiled({
      name,
      component,
      path,
      meta,
      userId,
    })) {
      return false;
    }
    let res = await service.admin.user.MenuModify(params);
    ctx.body = res;

  }
  /**
   * 删除菜单
   */
  async menuDelete() {
    const {
      ctx,
      service
    } = this, { id, userId } = ctx.request.body;
    if (!ctx.helper.isNullFiled({ id, userId })) {
      return
    }
    let res = await service.admin.user.menuDelete({ id, userId });
    ctx.body = res
  }

  /**
 * 角色类型列表
 */
  async roleTypeList() {
    const {
      ctx,
      service
    } = this, { userId, page, pageSize, role_name, aroleid, isAll } = ctx.request.body;
    if (!ctx.helper.isNullFiled({ userId })) {
      return
    }
    let res = await service.admin.user.roleTypeList({ userId, page, pageSize, role_name, aroleid, isAll });
    ctx.body = res
  }
  /**
 * 角色类型更新
 */
  async roleTypeUpData() {
    const {
      ctx,
      service
    } = this, { userId, aroleid, role_name, listorder, description, operationType } = ctx.request.body;
    if (!ctx.helper.isNullFiled({ userId, aroleid })) {
      return
    }
    let res = await service.admin.user.roleTypeUpData({ userId, aroleid, role_name, listorder, description, operationType });
    ctx.body = res
  }

  /**
 * 角色类型更新
 */
  async addRoleType() {
    const {
      ctx,
      service
    } = this, { userId, role_name, listorder, description } = ctx.request.body;
    if (!ctx.helper.isNullFiled({ userId, role_name, description })) {
      return
    }
    let res = await service.admin.user.addRoleType({ userId, role_name, listorder, description });
    ctx.body = res
  }


  /**
   * 管理员列表
   */
  async userList() {
    const {
      ctx,
      service
    } = this,
      { userId, page, pageSize, userState, phone } = ctx.request.body;
    if (!ctx.helper.isNullFiled({ userId, page, pageSize, userState })) {
      return
    }
    let res = await service.admin.user.userList({ userId, page, pageSize, userState, phone });
    ctx.body = res
  }


}


module.exports = users